package com.candy.candyuser.extend;

import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Objects;

/**
 * @author suYanLong
 * @date 2023年02月14日 14:01
 */
@Component
public class EncryptUtilsImpl implements IEncryptUtil {

    @Override
    public <T> T encrypt(Field[] declaredFields, T paramObject) throws IllegalAccessException {
        // 取出resultType的类
        for (Field field : declaredFields) {
            //取出所有被DecryptTransaction注解的字段
            EncryptTransaction encryptTransaction = field.getAnnotation(EncryptTransaction.class);
            if (!Objects.isNull(encryptTransaction)) {
                field.setAccessible(true);
                Object object = field.get(paramObject);//string的解密
                if (object instanceof String) {
                    String value = (String) object;//对注解的字段进行逐一解密
                    try {
                        field.set(paramObject, DBAESUtil.encrypt(value));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return paramObject;
    }

}
